package mdm.led.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mdm.led.common.AjaxResponse;
import mdm.led.common.BaseController;
import mdm.led.common.Constant;
import mdm.led.entity.AdminGetDope;
import mdm.led.entity.department.Department;
import mdm.led.service.DepartmentService;

@Controller
@RequestMapping(value = "department")
@Api(description = "部门管理")
public class DepartmentController extends BaseController {

	@Autowired
	private DepartmentService departmentService;

	private static final Logger LOGGER = LoggerFactory.getLogger(DepartmentController.class);

	@ApiOperation("添加部门")
	@ResponseBody
	@RequestMapping(value = "addDepartment", method = RequestMethod.POST)
	public AjaxResponse addDepartment(@RequestBody Department department) {
		boolean iRet = false;

		try {
			departmentService.addDepartment(department);
			iRet = true;
		} catch (Exception e) {
			LOGGER.info("\n出现异常=====\n{}", e);
		}

		if (iRet) {
			return AjaxResponse.reponseBody(100, "SUCCESS", null);
		} else {
			return AjaxResponse.reponseBody(101, "ERROR", null);
		}
	}

	@ApiOperation("修改部门")
	@ResponseBody
	@RequestMapping(value = "updateDepartment", method = RequestMethod.POST)
	public AjaxResponse updateDepartment(@RequestBody Department department) {
		boolean iRet = false;

		try {
			departmentService.updetaDepartment(department);
			iRet = true;
		} catch (Exception e) {
			e.printStackTrace();
		}

		if (iRet) {
			return AjaxResponse.reponseBody(100, "SUCCESS", null);
		} else {
			return AjaxResponse.reponseBody(101, "ERROR", null);
		}
	}

	@ApiOperation(value = "查询匹配部门", notes = "1:name 模糊匹配部门名称, <br>2:type 部门类型(1:支队 2:科室 3:大队 4 :中队),<br>3:fathername 上级部门名城管模糊匹配 , <br>4:fatherid 上级部门精确匹配")
	@ResponseBody
	@RequestMapping(value = "findDepartment", method = RequestMethod.POST)
	public AjaxResponse findDepartment(@RequestBody Department department, @RequestParam("pageSize") Integer pageSize,
			@RequestParam("currPage") Integer currPage) {
		// 开启分页
		Page<Department> page = PageHelper.startPage(currPage, pageSize);
		List<Department> list = departmentService.findDepartment(department);
		// 需要返回的 map 数据集合
		Map<String, Object> data = new HashMap<String, Object>();
		// 数据
		data.put("inspectorList", list);
		// 当前页
		data.put("currPage", page.getPageNum());
		// 页总数
		data.put("pageCount", page.getPages());
		// 页大小
		data.put("pageSize", page.getPageSize());
		// 总条数
		data.put("totalCount", page.getTotal());
		LOGGER.info("\n-------------- 结果:" + JSON.toJSONString(data) + " ------------");
		return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, data);

	}

	@ApiOperation(value = "查询单个部门")
	@ResponseBody
	@RequestMapping(value = "findDepartmentById", method = RequestMethod.GET)
	public AjaxResponse findDepartmentById(@RequestParam("id") Integer id) {

		Department findDepartmentById = departmentService.findDepartmentById(id);
		LOGGER.info("\n-------------- 结果:" + JSON.toJSONString(findDepartmentById) + " ------------");
		return AjaxResponse.reponseBody(Constant.SUCCESS_CODE, Constant.SUCCESS_MSG, findDepartmentById);

	}

}
